import requests
from lxml import etree
from modules.utils.log_util import LOGGER

"""
抓取百度热搜
"""
baseurl = "https://top.baidu.com/board?tab=realtime&sa=fyb_realtime_31065"
header = {
    "Referer": "https://www.baidu.com/s?ie=UTF-8&wd=%E7%99%BE%E5%BA%A6%E7%83%AD%E6%90%9C",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
}


def get_baidu_data():
    data = []

    LOGGER.info("请求百度热搜数据")

    try:
        response = requests.get(baseurl, headers=header, timeout=10)
        html = response.text
    except Exception as e:
        LOGGER.error(e)
        LOGGER.error("百度热搜请求失败")
    else:
        try:
            tree = etree.HTML(html)
            category_wrap = tree.xpath("//*[contains(@class,'category-wrap')]")
            for node in category_wrap:
                item = {}
                title = node.xpath(".//div[contains(@class,'c-single-text-ellipsis')]")[0].text
                hot_desc = node.xpath(".//div[contains(@class,'hot-desc')]")[0].text
                url = node.xpath(".//div[contains(@class,'content')]/a/@href")[0]
                hot_index = node.xpath(".//div[contains(@class,'hot-index')]")[0].text
                image = node.xpath(".//img/@src")[-3]
                item["title"] = title
                item["hot_index"] = hot_index
                item["hot_desc"] = hot_desc
                item["image"] = image
                item["url"] = url
                data.append(item)
        except Exception as e:
            LOGGER.error(e)
            LOGGER.error("百度热搜数据解析失败")
    return data


if __name__ == "__main__":
    print(get_baidu_data())
